(*
  Copyright (c) 2009 Barry Schwartz

  Permission is hereby granted, free of charge, to any person
  obtaining a copy of this software and associated documentation
  files (the "Software"), to deal in the Software without
  restriction, including without limitation the rights to use,
  copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the
  Software is furnished to do so, subject to the following
  conditions:

  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  OTHER DEALINGS IN THE SOFTWARE.
*)

open UTypes
  
(* implementation of a table with a selected element *)
type 'a table = {
  current : 'a;                  (* the selected element *)
  key     : uc_string;           (* the key of the selected element *)
  table   : 'a DynUCTrie.t       (* the whole table *)
}

(* |create <key> <element>| creates a table containing the pair <key>, <element>. *)
let create key element = {
  current = element;
  key     = key;
  table   = DynUCTrie.add_string key element DynUCTrie.empty;
}
  
(* |sync <t>| adds the current element back to the table. *)
let sync t = { t with table = DynUCTrie.add_string t.key t.current t.table }
  
(* |current <table>| returns the current element and |key <table>| returns its key. *)
let current t = t.current
  
let key t = t.key
  
(* |table <table|| returns the table. *)
let table t = (sync t).table
  
(* |get <table> <key>| searchs for <key> without affecting the current element. *)
let get table key =
  let t = sync table in
    DynUCTrie.find_string key t.table
  
(* |select <table> <key>| sets the selected element. *)
let select table key =
  let t = sync table in
    { t with
        key = key;
        current = DynUCTrie.find_string key t.table }
  
(* |add <table> <key> <element>| adds a new elemement to the table. *)
let add table key element =
  let t = sync table in
    { key = key;
      current = element;
      table = DynUCTrie.add_string key element t.table }
  
(* |set <table> <element>| modifies the selected element without updating the whole table. *)
let set table element = { table with current = element }
  
(* |update <table> <t>| replaces the table <table> with <t>. *)
let update table t =
  { key = table.key;
    current = DynUCTrie.find_string table.key t;
    table = t }
  
(* |map <table> <f>| applies <f> to all elements of <table>. *)
let map table f =
  let t = sync table in
    update t (DynUCTrie.map f t.table)
  
(* |mapi <table> <f>| applies <f> to all elements of <table>. *)
let mapi table f =
  let t = sync table in
    update t (DynUCTrie.mapi f t.table)
